package controller

import (
	"ginblog/enum"
	"ginblog/model"
	"ginblog/resp"
	"ginblog/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type CommentController struct {
	comm service.CommentService
	r    resp.Resp
}

type CommentConInterface interface {
	GetCommentList(c *gin.Context)
	DeleteComment(context *gin.Context)
	CheckComment(c *gin.Context)
	GetComment(c *gin.Context)
	UncheckComment(c *gin.Context)
	CommentFront(c *gin.Context)
	AddComment(c *gin.Context)
}

// GetCommentList 后台查询评论列表
func (comm CommentController) GetCommentList(c *gin.Context) {
	// 获取用户传输的数据
	// 获取前端传过来的数据 并转换给int类型
	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	pageNum, _ := strconv.Atoi(c.Query("pageNum"))
	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	// 判断数据
	if pageNum == 0 {
		pageNum = -1
	}
	commontlist, total, code := comm.comm.GetCommentList(pageSize, pageNum)
	if code != enum.SUCCESS {
		comm.r.RespData(code, code.String(), nil, c)
	}
	c.JSON(http.StatusOK, gin.H{
		"status":  enum.SUCCESS,
		"message": enum.SUCCESS.String(),
		"data":    commontlist,
		"total":   total,
	})
}

// DeleteComment 删除评论
func (comm CommentController) DeleteComment(c *gin.Context) {
	// 获取前端传递的参数
	// 获取id
	commId, _ := strconv.Atoi(c.Param("id"))
	code := comm.comm.DeleteUser(commId)
	if code != enum.SUCCESS {
		comm.r.RespData(code, code.String(), nil, c)
	}
	comm.r.RespData(code, code.String(), "删除成功！", c)
}

// CheckComment 通过审核
func (comm CommentController) CheckComment(c *gin.Context) {
	// 获取前端传递的参数
	var data model.Comment
	_ = c.ShouldBindJSON(&data)
	// 获取id
	commId, _ := strconv.Atoi(c.Param("id"))

	code := comm.comm.CheckComment(commId, &data)
	if code != enum.SUCCESS {
		comm.r.RespData(code, code.String(), nil, c)
	}
	comm.r.RespData(code, code.String(), "通过审核", c)

}

// GetComment 查询单条评论
func (comm CommentController) GetComment(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	data, code := comm.comm.GetComment(id)
	if code != enum.SUCCESS {
		comm.r.RespData(code, code.String(), nil, c)
	}
	comm.r.RespData(code, code.String(), data, c)
}

// UncheckComment 撤下评论审核
func (comm CommentController) UncheckComment(c *gin.Context) {
	var data model.Comment
	_ = c.ShouldBindJSON(&data)
	id, _ := strconv.Atoi(c.Param("id"))

	code := comm.comm.UncheckComment(id, &data)
	if code != enum.SUCCESS {
		comm.r.RespData(code, code.String(), nil, c)
	}
	comm.r.RespData(code, code.String(), "撤销成功!", c)
}

func (comm CommentController) CommentFront(c *gin.Context) {
	commId, _ := strconv.Atoi(c.Param("id"))

	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
	pageNum, _ := strconv.Atoi(c.Query("pageNum"))

	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}
	// 判断数据
	if pageNum == 0 {
		pageNum = -1
	}
	if commId == 0 {
		comm.r.RespData(enum.ERROR, enum.ERROR.String(), "参数不合法", c)
	}
	comments, code, count := comm.comm.CommentFront(commId, pageSize, pageNum)
	if code != enum.SUCCESS {
		comm.r.RespData(code, code.String(), nil, c)
	}
	c.JSON(http.StatusOK, gin.H{
		"status":  enum.SUCCESS,
		"message": enum.SUCCESS.String(),
		"data":    comments,
		"total":   count,
	})
}

func (comm CommentController) AddComment(c *gin.Context) {
	// 获取修改的数据
	comment := model.Comment{}
	err := c.ShouldBind(&comment)
	if err != nil {
		comm.r.RespData(enum.ERROR, enum.ERROR.String(), "数据格式有误！", c)
	}

	code := comm.comm.AddComment(&comment)
	if code != enum.SUCCESS {
		comm.r.RespData(code, code.String(), nil, c)
	}
	comm.r.RespData(code, code.String(), "发布成功!", c)
}
